package ex2.grammars.factories;

import ex2.Token;
import ic.ast.expr.BinaryOp;
import ic.ast.expr.Expression;

/**
 * Created with IntelliJ IDEA.
 * User: Roi
 * Date: 11/25/13
 */
public class BinaryOpFactory {

    /**
     * Creates binary operation according to given parameters
     * @param token token
     * @param lhs left expression
     * @param rhs right expression
     * @return created binary operation
     */
    public static BinaryOp create(Token token, Expression lhs, Expression rhs) {
        if(token.getValue().equals("=="))
            return new BinaryOp(token.getLine(), lhs, BinaryOp.BinaryOps.EQUAL, rhs);
        else if(token.getValue().equals("!="))
            return new BinaryOp(token.getLine(), lhs, BinaryOp.BinaryOps.NEQUAL, rhs);
        else if(token.getValue().equals("<"))
            return new BinaryOp(token.getLine(), lhs, BinaryOp.BinaryOps.LT, rhs);
        else if(token.getValue().equals("<="))
            return new BinaryOp(token.getLine(), lhs, BinaryOp.BinaryOps.LTE, rhs);
        else if(token.getValue().equals(">"))
            return new BinaryOp(token.getLine(), lhs, BinaryOp.BinaryOps.GT, rhs);
        else if(token.getValue().equals(">="))
            return new BinaryOp(token.getLine(), lhs, BinaryOp.BinaryOps.GTE, rhs);
        else if(token.getValue().equals("+"))
            return new BinaryOp(token.getLine(), lhs, BinaryOp.BinaryOps.PLUS, rhs);
        else if(token.getValue().equals("-"))
            return new BinaryOp(token.getLine(), lhs, BinaryOp.BinaryOps.MINUS, rhs);
        else if(token.getValue().equals("*"))
            return new BinaryOp(token.getLine(), lhs, BinaryOp.BinaryOps.MULTIPLY, rhs);
        else if(token.getValue().equals("/"))
            return new BinaryOp(token.getLine(), lhs, BinaryOp.BinaryOps.DIVIDE, rhs);
        else if(token.getValue().equals("%"))
            return new BinaryOp(token.getLine(), lhs, BinaryOp.BinaryOps.MOD, rhs);
        else
            throw new Error("Error");
    }
}
